//
//  ReadBookViewController.m
//  ReadBook
//
//  Created by Mac on 15-12-11.
//
//

#import "ReadBookViewController.h"
#import "ContenViewController.h"
@interface ReadBookViewController () <UIPageViewControllerDataSource,UIPageViewControllerDelegate>
{
    UIPageViewController *pageViewCon;
    UIToolbar *toolBar;
    CGFloat totalHeight;
    NSUInteger totalPages;
    NSUInteger curPage;
    NSUInteger lenPerPage;
    UIBarButtonItem *barItem;
}

@end

@implementation ReadBookViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    pageViewCon = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey:@(UIPageViewControllerSpineLocationNone)}];
    pageViewCon.dataSource = self;
    pageViewCon.delegate = self;
    [self addChildViewController:pageViewCon];
    pageViewCon.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 -44);
    [self.view addSubview:pageViewCon.view];
    toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
    [self.view addSubview:toolBar];
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[[UILabel new].font fontWithSize:24.0]} context:nil];
    totalHeight = ceilf(rect.size.height);
    totalPages = ceilf(totalHeight / (self.view.frame.size.height - 64 -44));
    lenPerPage = _text.length / totalPages;
    curPage = 0;
    
    barItem = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"第%u页/共%u页",curPage + 1,totalPages] style:UIBarButtonItemStylePlain target:nil action:nil];
    toolBar.items = @[barItem];
    
    ContenViewController *con = [ContenViewController new];
    con.text = [_text substringWithRange:NSMakeRange(curPage * lenPerPage, lenPerPage)];
    [pageViewCon setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

- (void)refreshBarItemShow
{
    barItem.title = [NSString stringWithFormat:@"第%u页／共%u页",curPage + 1, totalPages];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (curPage <= 0) {
        return nil;
    }
    curPage--;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (curPage >= totalPages - 1) {
        return nil;
    }
    curPage++;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (UIViewController *)getCurContentController
{
    ContenViewController *con = [ContenViewController new];
    con.text = [_text substringWithRange:NSMakeRange(curPage * lenPerPage, lenPerPage)];
    con.index = curPage;
    return con;
}


- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    //    NSLog(@"animate finished? %d, trans completed? %d", finished, completed);
    if (!completed) {
        ContenViewController *con = (ContenViewController *)previousViewControllers[0];
        curPage = con.index;
        [self refreshBarItemShow];
    }
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
